﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraNavBar;
using Model;

namespace TaxiDispatchClient
{
    public partial class RoleManagementForm : DevExpress.XtraEditors.XtraForm
    {
        public RoleManagementForm()
        {
            InitializeComponent();
            RefreshList();
        }

        private NavBarItemLink _lastItem;
        private void RefreshList()
        {
            navBarGroup1.ItemLinks.Clear();
            DataSet ds = MainForm.PositionService.RoleGetList("");
            navBarGroup1.ItemLinks.Clear();
            if (ds == null || ds.Tables[0].Rows.Count < 1)
            {
                ClearDisplay();
                _lastItem = null;
                return;
            }
            ClearDisplay();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {               
                NavBarItem item = new NavBarItem(ds.Tables[0].Rows[i]["RoleName"].ToString());
                item.Tag = ds.Tables[0].Rows[i];
               // item.LargeImage = Properties.Resources.userGroupIcon_01;
                navBarGroup1.ItemLinks.Add(item);
                if (_lastItem != null)
                {
                    DataRow dr = _lastItem.Item.Tag as DataRow;
                    if (ds.Tables[0].Rows[i]["id"].ToString() == dr["id"].ToString())
                    {
                        _lastItem.Item.Tag = item.Tag;
                    }
                }
            }        
            if (_lastItem != null)
            {
                navBarControl1_LinkClicked(null, new NavBarLinkEventArgs(_lastItem));
            }
            else
            {
                navBarControl1_LinkClicked(null, new NavBarLinkEventArgs(navBarGroup1.ItemLinks[0]));
            }   
        }

        //清除显示
        private void ClearDisplay()
        {

            textEdit_groupname.Text = "";
            textEdit_groupname.Tag = null;
            btn_deleteGroup.Visible = false;
            memoEdit_remark.Text = "";
            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
               checkedListBox1.Items[i].CheckState = CheckState.Unchecked;
            }            
        }

        private void navBarControl1_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            DataRow group = e.Link.Item.Tag as DataRow;
            string permission = group["Permission"].ToString()+"|";
            textEdit_groupname.Text = group["RoleName"].ToString();          
            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                if (permission.Contains("|"+
                    checkedListBox1.Items[i].Value + "|"))
                {
                    checkedListBox1.Items[i].CheckState = CheckState.Checked;
                }
                else
                {
                    checkedListBox1.Items[i].CheckState = CheckState.Unchecked;
                }
            }
            textEdit_groupname.Tag = int.Parse(group["id"].ToString());
            memoEdit_remark.Text = group["Remark"].ToString();
            btn_save.Enabled = true;
            btn_save.Tag = "修改";
            btn_save.Image = Properties.Resources.UserForm_btnmodify001;
            _lastItem = e.Link;
            btn_deleteGroup.Visible = true;
            //this.txtGroupName.Visible = true;            
        }

        private void btn_save_Click(object sender, EventArgs e)
        {            
            if( btn_save.Tag == "添加")
            {
                //新增分组
                if (textEdit_groupname.Tag == null || textEdit_groupname.Tag.ToString() == "")
                {
                    if (CheckValuePassed())
                    {
                        RoleInfo role = new RoleInfo();
                        role.RoleName = textEdit_groupname.Text;
                        role.Remark = memoEdit_remark.Text;
                        string permission = "";
                        for (int i = 0; i < checkedListBox1.Items.Count; i++)
                        {
                            if (checkedListBox1.Items[i].CheckState == CheckState.Checked)
                            {
                                permission += "|" + checkedListBox1.Items[i].Value+"|";
                            }
                        }
                        role.Permission = permission;
                        if (MainForm.PositionService.RoleAdd(role))
                        {
                            XtraMessageBox.Show("添加组成功！");                                       
                            navBarControl1.Enabled = true;
                            _lastItem = null;
                            RefreshList();
                            btn_cancel.Visible = false;
                            btn_addGroup.Visible = true;
                        }
                    }
                }
            }
            else //修改分组
            {
                if (!CheckValuePassed()) return;
                if(XtraMessageBox.Show("确认修改该分组的信息？","警告:",MessageBoxButtons.YesNo)
                    == DialogResult.Yes)
                {
                    string permission = "";
                    for (int i = 0; i < checkedListBox1.Items.Count; i++)
                    {
                        if (checkedListBox1.Items[i].CheckState == CheckState.Checked)
                        {
                            permission += "|" + checkedListBox1.Items[i].Value + "|";
                        }
                    }
                    int id = (int)textEdit_groupname.Tag;
                    string name = textEdit_groupname.Text;
                    string remark = memoEdit_remark.Text;
                    if (MainForm.PositionService.RoleUpdate(id, name, permission, remark))
                    {
                        XtraMessageBox.Show("修改成功！");
                        RefreshList();
                    }     
                }
                else
                {
                    RefreshList();
                }
                                   
            }                                        
        }

        private void RoleManagementForm_Load(object sender, EventArgs e)
        {

        }

        private void btn_addGroup_Click(object sender, EventArgs e)
        {          
            ClearDisplay();
            navBarControl1.Enabled = false;           
            btn_save.Tag = "添加";
            btn_save.Image = Properties.Resources.UserForm_btnsave001;
            btn_cancel.Visible = true;
            btn_addGroup.Visible = false;
        }
        private bool CheckValuePassed()
        {
            if(textEdit_groupname.Text=="")
            {
                XtraMessageBox.Show("组名称不能为空！");
                textEdit_groupname.Focus();
                return false;
            }
            if(Encoding.GetEncoding("GBK").GetBytes(textEdit_groupname.Text).Length > 20)
            {
                XtraMessageBox.Show("组名长度过长！");
                textEdit_groupname.Focus();
                return false;
            }
            return true;
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            RefreshList();
            btn_cancel.Visible = false;
            navBarControl1.Enabled = true;
            btn_addGroup.Visible = true;
        }

        private void btn_deleteGroup_Click(object sender, EventArgs e)
        {
            if(_lastItem!=null && XtraMessageBox.Show("确定删除用户组:"+_lastItem.Item.Caption+
                "吗？","删除",MessageBoxButtons.YesNo)== DialogResult.Yes)
            {
                int id = int.Parse(textEdit_groupname.Tag.ToString());
                if(id>-1)
                {
                    if (MainForm.PositionService.RoleDelete(id))
                    {
                        _lastItem = null;
                        RefreshList();                        
                    }
                    else
                    {
                        XtraMessageBox.Show("服务器通讯故障，删除失败，请重试！");
                    }
                    //if (navBarGroup1.ItemLinks.Count > 0)
                    //{
                    // navBarControl1_LinkClicked(null, new NavBarLinkEventArgs(navBarGroup1.ItemLinks[0]));
                    //}
                }
            }
        }

        #region 窗体控件效果
        Point _mouseOff;
        bool _leftFlag;
        private void Form_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _mouseOff = new Point(-e.X, -e.Y);
                _leftFlag = true;
            }
        }

        private void Form_MouseMove(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(_mouseOff.X, _mouseOff.Y);
                Location = mouseSet;
            }
        }

        private void Form_MouseUp(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                _leftFlag = false;
            }
        }

        private void pictureEdit_exit_MouseHover(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexit;
            Cursor = Cursors.Hand;
        }

        private void pictureEdit_exit_MouseLeave(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexitPressDown;

            Cursor = Cursors.Arrow;

        }
        private void btn_MouseHover(object sender, EventArgs e)
        {
            Cursor = Cursors.Hand;
        }

        private void btn_MouseLeave(object sender, EventArgs e)
        {
            Cursor = Cursors.Arrow;
        }

        private void pictureEdit_exit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Form_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                Close();
            }
        }
        #endregion

       
    }
}